<template>
  <div class="box">
   <headers :head_title="head_title" />
    <div class="content_socket">
      <div class="socket">

        <div class="chat">
          <div class="message-item" v-for="(item, ind) in messageList" :key="ind">
            <div class="flex-left" v-if="item.type === 'server'">
              <div class="avatar">
                <img src="../../image/医生logo.png" width="100%" height="100%" alt="" />
              </div>
              <div class="message-content">
                {{ item.content }}
              </div>
            </div>
            <div class="flex-right" v-else>
              <div class="message-content">
                <div v-if="item.content.startsWith('data')">
                  <img :src="item.content" style="width: 300px; height: 190px" />
                </div>
                <div v-else>
                  {{ item.content }}
                </div>
              </div>
              <div class="avatar">
                <img src="../../image/my_img/my_user.jpg" width="100%" height="100%" alt="" />
              </div>
            </div>
          </div>
        </div>
        <div class="footer" v-if="showFlag === 1">
          <img src="../../image//手机键盘.png"  class="footer_img">
          <textarea
            v-model="yourMessage"
            placeholder="请输入你要查询的内容"
          ></textarea>
          <button @click="sendMessage">提交</button>
          <el-icon size="60" @click="sendimg" class="footer_img"><PictureRounded /></el-icon>
        </div>
        <div class="fileImage" v-else-if="showFlag === 2">
          <div
            style="
              display: flex;
              justify-content: space-between;
              align-items: center;
              height: 50px;
              line-height: 30px;
            "
          >
            <div style="display: flex">
              <h3 style="font-size: 32px">上传图片</h3>
              <span style="color: #999; padding-left: 5px; padding-top: 2px">(选填)</span>
            </div>
            <p class="Tiao">跳过</p>
          </div>
          <p style="color: #999; letter-spacing: 3px">请选择需要的照片(最多5张)</p>
          <div style="margin-top: 15px; margin-bottom: 10px">
            <van-uploader v-model="fileList" multiple :after-read="afterUpload" />
          </div>
          <button class="FileBtn" @click="SendSubmit">确定</button>
        </div>
        <div class="footer" v-else>
          <textarea
            v-model="yourMessage"
            placeholder="请输入你要查询的内容"
          ></textarea>
          <button @click="sendMessage">提交</button>
        </div>
      </div>
    </div>
  </div>
</template>


<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import headers from '@/components/header.vue'
//head头部数据
const head_title = ref('小美')

const userInfo = ref({
  userId: '',
})

const fileList = ref<any>([])
const fileImageList = ref<any>([])

const showFlag = ref(1)

const messageList = ref([
  {
    type: 'server',
    content: '您好,我是您的医生助手',
  },
  {
    type: 'server',
    content: '请在下方输入框详细描述病情,以便医生准确了解您的情况,快速接诊',
  },
])

const afterUpload = (file: any) => {
  console.log(file.content)
  fileImageList.value.push(file.content)
  console.log('base64路径', file.content)
  console.log(fileImageList.value)
}

console.log(fileList.value)
const yourMessage = ref('')

let ws = null

const sendimg = () => {
  showFlag.value = 2
 }

const sendMessage = () => {
  messageList.value.push({
    type: 'message',
    content: yourMessage.value,
  })
  const jsonMessage = {
    imageBase64: yourMessage.value,
  }
  ws.send(JSON.stringify(jsonMessage))
  yourMessage.value = ''
}

const SendSubmit = () => {
  showFlag.value = 1
  messageList.value.push({
    type: 'message',
    content: fileImageList.value[0],
  })
  console.log(typeof fileImageList.value)
  const jsonData = {
    imageBase64: fileImageList.value[0],
  }
  ws.send(JSON.stringify(jsonData))

  fileImageList.value = []
  fileList.value = []
}

const initWebsocket = () => {
  ws = new WebSocket('ws://localhost:8080/')
  console.log(ws)
  ws.onopen = () => {
    console.log('开始聊天')
  }
  ws.onmessage = (event) => {
    console.log(event.data)
    messageList.value.push({
      type: 'server',
      content: event.data,
    })
  }
  ws.onerror = (error) => {
    console.error('websocket', error)
  }
}

onMounted(() => {
  userInfo.value.userId = new Date().getTime().toString().slice(8)
  initWebsocket()
})
</script>


<style lang="scss" scoped>
.box {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.content_socket {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  width: 100%;
  height: 602px; // 1204px * 0.5 = 602px
  background-color: #eee;
 .socket {
    flex: 1;
    background-color: #eee;
  }
}

.chat {
  width: 90%;
  height: 90%;
  // background: red;
  overflow: auto;
  margin: 0 auto;
  margin-top: 15px; // 30px * 0.5 = 15px
}

.message-item {
  width: 80%;
  margin: 0 auto;
  margin-bottom: 5px; // 10px * 0.5 = 5px
}

.flex-left {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  margin-left: -30px; // 60px * 0.5 = 30px
  margin-bottom: 12.5px; // 25px * 0.5 = 12.5px
}

.avatar {
  width: 35px; // 70px * 0.5 = 35px
  height: 35px; // 70px * 0.5 = 35px
  border-radius: 50%;
  overflow: hidden;
  display: flex;
  margin-right: 6px; // 12px * 0.5 = 6px
  background-color: #fafafa;
}

.message-content {
  min-height: 30px; // 60px * 0.5 = 30px
  max-width: 220px; // 440px * 0.5 = 220px
  background-color: #fff;
  border-radius: 6px; // 12px * 0.5 = 6px
  padding: 10px; // 20px * 0.5 = 10px
  letter-spacing: 1.5px; // 3px * 0.5 = 1.5px
  font-size: 13.5px; // 27px * 0.5 = 13.5px
}

.flex-right {
  margin-top: 12.5px; // 25px * 0.5 = 12.5px
  display: flex;
  justify-content: flex-end;
  margin-right: -30px; // 60px * 0.5 = 30px
  overflow-x: hidden;
}

.footer {
  width: 375px; // 750px * 0.5 = 375px
  height: 43.5px; // 87px * 0.5 = 43.5px
  display: flex;
  align-items: center;
  background-color: #fff;
  position: fixed;
  bottom: 0px;
  textarea {
    padding: 2.5px 5px; // 5px 10px分别减半
    outline: none;
    cursor: pointer;
    font-size: 13px; // 26px * 0.5 = 13px
    font-family: 'Times New Roman', serif;
    letter-spacing: 1.5px; // 3px * 0.5 = 1.5px
    height: 70%;
    flex: 1;
    background-color: #eee;
    border-radius: 10px; // 20px * 0.5 = 10px
    margin: 0 10px; // 20px减半
  }
  button {
    font-size: 12.5px; // 25px * 0.5 = 12.5px
    border: none;
    color: white;
    border-radius: 10px; // 20px * 0.5 = 10px
    background-color: #368beb;
    padding: 10px 15px; // 20px 30px分别减半
    margin-right: 10px; // 20px * 0.5 = 10px
  }
 .footer_img {
    width: 30px; // 60px * 0.5 = 30px
    height: 30px; // 60px * 0.5 = 30px
    margin-left: 7.5px; // 15px * 0.5 = 7.5px
  }
 .footer_img {
    margin-right: 7.5px; // 15px * 0.5 = 7.5px
  }
}

.fileImage {
  background-color: #fff;
  width: 667px; // 1334px * 0.5 = 667px
  height: 375px; // 750px * 0.5 = 375px
  display: flex;
  flex-direction: column;
  padding: 6px 10px; // 12px 20px分别减半
  position: fixed;
  bottom: -12.5%; // 25% * 0.5 = 12.5%
}

.Tiao {
  position: absolute;
  right: 5%;
  top: 5%;
}

.FileBtn {
  width: 55%;
  border-radius: 12.5px; // 25px * 0.5 = 12.5px
  height: 40px; // 80px * 0.5 = 40px
  border: none;
  color: white;
  font-size: 15px; // 30px * 0.5 = 15px
  background-color: #368beb;
  line-height: 25px; // 50px * 0.5 = 25px
}
</style>
